// Decompiled by DJ v3.10.10.93 Copyright 2007 Atanas Neshkov  Date: 27/11/2008 22:59:12
// Home Page: http://members.fortunecity.com/neshkov/dj.html  http://www.neshkov.com/dj.html - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   Node.java

package reversi; 

import java.io.PrintStream;
import java.util.Vector;

// Referenced classes of package reversi:
//            Move, GameState

public class Node
{

    public Node()
    {
        lastMoveX = -1;
        lastMoveY = -1;
        lastMovePlayer = -1;
        children = new Vector();
        parent = null;
        state = null;
        score = 0.0D;
    }

    public Node(GameState gamestate, Move move)
    {
        lastMoveX = -1;
        lastMoveY = -1;
        lastMovePlayer = -1;
        children = new Vector();
        parent = null;
        state = null;
        score = 0.0D;
        state = gamestate;
        if(move != null)
            setMove(move);
    }

    public Node(GameState gamestate, int i, int j, int k)
    {
        lastMoveX = -1;
        lastMoveY = -1;
        lastMovePlayer = -1;
        children = new Vector();
        parent = null;
        state = null;
        score = 0.0D;
        state = gamestate;
        lastMoveX = i;
        lastMoveY = j;
        lastMovePlayer = k;
    }

    public int getDepth()
    {
        return getDepth(1);
    }

    int getDepth(int i)
    {
        if(children.size() == 0)
            return i;
        int j = 0;
        for(int k = 0; k < children.size(); k++)
        {
            Node node = (Node)children.elementAt(k);
            j = Math.max(j, node.getDepth(i + 1));
        }

        return j;
    }

    public void print()
    {
        print(0);
    }

    void print(int i)
    {
        for(int j = 0; j < i; j++)
            System.out.print("  ");

        System.out.println(score);
        for(int k = 0; k < children.size(); k++)
            ((Node)children.elementAt(k)).print(i + 1);

    }

    public boolean isParent(Node node)
    {
        return parent == node || parent != null && parent.isParent(node);
    }

    public Move getMove()
    {
        return new Move(lastMoveX, lastMoveY, lastMovePlayer);
    }

    public void setMove(Move move)
    {
        lastMoveX = move.x;
        lastMoveY = move.y;
        lastMovePlayer = move.player;
    }

    public void setState(GameState gamestate)
    {
        state = gamestate;
    }

    public GameState getState()
    {
        return state;
    }

    public void setParent(Node node)
    {
        if(parent != null)
            parent.removeChild(this);
        parent = node;
    }

    public Node getParent()
    {
        return parent;
    }

    public void addChild(Node node)
    {
        if(!children.contains(node))
        {
            children.addElement(node);
            node.setParent(this);
        }
    }

    public void removeChild(Node node)
    {
        children.removeElement(node);
        node.parent = null;
    }

    public Vector getChildren()
    {
        return children;
    }

    public boolean hasChildren()
    {
        return children.size() > 0;
    }

    public void setScore(double d)
    {
        score = d;
    }

    public double getScore()
    {
        return score;
    }

    public Node getOptimalChild()
    {
        for(int i = 0; i < children.size(); i++)
        {
            Node node = (Node)children.elementAt(i);
            if(node.score == score)
                return node;
        }

        return null;
    }

    public void propagateScore()
    {
        propagateScore(true);
    }

    public void propagateScore(boolean flag)
    {
        if(flag)
        {
            double d = -1.7976931348623157E+308D;
            for(int i = 0; i < children.size(); i++)
            {
                Node node = (Node)children.elementAt(i);
                if(node.score > d)
                    d = node.score;
            }

            score = d;
        } else
        {
            double d1 = 1.7976931348623157E+308D;
            for(int j = 0; j < children.size(); j++)
            {
                Node node1 = (Node)children.elementAt(j);
                if(node1.score < d1)
                    d1 = node1.score;
            }

            score = d1;
        }
        if(parent != null)
            parent.propagateScore(!flag);
    }

    int lastMoveX;
    int lastMoveY;
    int lastMovePlayer;
    protected Vector children;
    protected Node parent;
    protected GameState state;
    protected double score;
}